package com.cn.demo4.dao.impl;

import com.cn.demo4.dao.ProductDao;
import com.cn.demo4.model.PageModel;
import com.cn.demo4.model.ProductModel;
import com.cn.demo4.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    @Override
    public List<ProductModel> findAll(ProductModel productModel) {
//        String sql = "select p.*,h.name house_name from product p, house h where p.house_id = h.id order by id desc";
        StringBuilder sql = new StringBuilder();
        sql.append("select p.*,h.name house_name from product p, house h where true and p.house_id = h.id");
        if (!"".equals(productModel.getHouseName())) {
            sql.append(" and h.name like '%").append(productModel.getHouseName()).append("%'");
        }
        if (!"".equals(productModel.getName())) {
            sql.append(" and p.name like '%").append(productModel.getName()).append("%'");
        }
        if (productModel.getStartPrice() != 0.0 && productModel.getEndPrice() == 0.0) {
            sql.append(" and market_price >= ").append(productModel.getStartPrice());
        }
        if (productModel.getStartPrice() ==0.0 && productModel.getEndPrice() != 0.0){
            sql.append(" and market_price <= ").append(productModel.getEndPrice());
        }
        if (productModel.getStartPrice() !=0.0  && productModel.getEndPrice() !=0.0 ) {
            if (!"".equals(productModel.getStartPrice()) && !"".equals(productModel.getEndPrice())) {
                sql.append(" and market_price between ").append(productModel.getStartPrice()).append(" and ").append(productModel.getEndPrice());
            }
        }
        if (productModel.getStatus() != -1) {
            if (productModel.getStatus() == 1) {
                sql.append(" and p.status=1");
            } else {
                sql.append(" and p.status=0 or p.status is null");
            }
        }
        sql.append(" order by p.id desc");
        sql.append(" limit ").append((productModel.getPageModel().getCurPage() - 1) * productModel.getPageModel().getPageSize()).append(",").append(productModel.getPageModel().getPageSize());
        ResultSet resultSet = JDBC.select(sql.toString());
        //3.因为不知道数据又多少行，所以先定义一个空集合来存放
        return getData(resultSet);
    }

    @Override
    public int add(ProductModel productModel) {
        String sql = "insert into product(" +
                "name,market_price,normal_price,description,img,is_show," +
                "house_id,status,create_time,update_time)" +
                "values('" + productModel.getName() + "'," +
                "" + productModel.getMarketPrice() + "," +
                "" + productModel.getNormalPrice() + "," +
                "'" + productModel.getDescription() + "'," +
                "'" + productModel.getImg() + "'," +
                "" + productModel.getIsShow() + "," +
                "" + productModel.getHouseId() + "," +
                "" + productModel.getStatus() + "," +
//                "" + productModel.getExample() + "," +
//                "" + productModel.getNavImg() + "," +
                "now(),now())";
        return JDBC.upDate(sql);
    }

    /**
     * 删除产品
     *
     * @param id
     * @return
     */
    @Override
    public int del(Integer id) {
        String sql = "delete from product where id=" + id;
        return JDBC.upDate(sql);
    }

    @Override
    public ProductModel findById(Integer id) {
        String sql = "select p.*,h.name house_name from product p, house h where p.house_id = h.id and p.id=" + id;
        ResultSet resultSet = JDBC.select(sql);
//        List<ProductModel> list = getData(resultSet);
//        return list.size() > 0 ? list.get(0) : null;
        return getData(resultSet).get(0);
    }

    @Override
    public int update(ProductModel productModel) {
        String sql = "update product set name='" + productModel.getName() + "'," +
                " market_price=" + productModel.getMarketPrice() + "," +
                " normal_price=" + productModel.getNormalPrice() + ",";
        if (!productModel.getDescription().equals("")) {
            sql += " description='" + productModel.getDescription() + "',";
        }
        if (productModel.getImg() != null && !productModel.getImg().equals("undefined")) {
            sql += " img='" + productModel.getImg() + "',";
        }
        sql+= " status="+productModel.getStatus()+",";
//        sql += " is_show=" + productModel.getIsShow() + ",";
        sql += " house_id=" + productModel.getHouseId() + ",";
        sql += "update_time=now()";
        sql += " where id=" + productModel.getId();
        return JDBC.upDate(sql);
    }

    @Override
    public int getCount(ProductModel productModel) {

        //1.sql查询语句
        StringBuilder sql = new StringBuilder();
        sql.append("select count(*) count, h.name house_name from  product p, house h where true and p.house_id = h.id");
        if (!"".equals(productModel.getName())) {
            sql.append(" and p.name like '%").append(productModel.getName()).append("%'");
        }
        if (!"".equals(productModel.getHouseName())) {
            sql.append(" and h.name like '%").append(productModel.getHouseName()).append("%'");
        }
        if (!"".equals(productModel.getStartPrice()) && "".equals(productModel.getEndPrice())) {
            sql.append(" and market_price >= ").append(productModel.getStartPrice());
        }
        if ("".equals(productModel.getStartPrice()) && !"".equals(productModel.getEndPrice())) {
            sql.append(" and market_price <= ").append(productModel.getEndPrice());
        }
        if (productModel.getStartPrice() != 0.0 && productModel.getEndPrice() != 0.0) {
            if (!"".equals(productModel.getStartPrice()) && !"".equals(productModel.getEndPrice())) {
                sql.append(" and market_price between ").append(productModel.getStartPrice()).append(" and ").append(productModel.getEndPrice());
            }
        }

        if (productModel.getStatus() != -1) {
            if (productModel.getStatus() == 1) {
                sql.append(" and p.status=1");
            } else {
                sql.append(" and p.status is null and p.status=0");
            }
        }
        sql.append(" order by p.id desc");
        ResultSet resultSet = JDBC.select(sql.toString());
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public List<ProductModel> getProductModelListForHome() {
        String sql = "select p.*,h.name house_name from product p, house h where p.house_id = h.id and p.status=1";
        ResultSet resultSet = JDBC.select(sql);
        //3.因为不知道数据又多少行，所以先定义一个空集合来存放
        return getData(resultSet);
    }


    /**
     * 公共方法查询数据
     *
     * @param resultSet
     * @return
     */
    private List<ProductModel> getData(ResultSet resultSet) {
        List<ProductModel> list = new ArrayList<>();
        //4.循环遍历数据，因为ResultSet有异常所以先写个try/catch语句
        try {
            while (resultSet.next()) {
                //5.因为最总要返回一个list，所以要调用UserModel
                ProductModel model = new ProductModel();//当前model为空值，所以要先赋值
                model.setId(resultSet.getInt("id"));//括号中的属性值对应数据库中的列名
                model.setName(resultSet.getString("name"));
                model.setMarketPrice(resultSet.getDouble("market_price"));
                model.setNormalPrice(resultSet.getDouble("normal_price"));
                model.setDescription(resultSet.getString("description"));
                model.setImg(resultSet.getString("img"));
                model.setIsRecommend(resultSet.getInt("is_recommend"));
                model.setIsShow(resultSet.getInt("is_show"));
                model.setCreateTime(resultSet.getString("create_time"));
                model.setUpdateTime(resultSet.getString("update_time"));
                model.setHouseId(resultSet.getInt("house_id"));
                model.setHouseName(resultSet.getString("house_name"));
//                model.setExample(resultSet.getString("example"));
//                model.setNavImg(resultSet.getString("nav_img"));
                model.setStatus(resultSet.getInt("status"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

}
